<?php defined('SMIT_PATH') or die('Access Denied');
/**
 *	dbMaster.php - Database implentation layer
 *	This is Smit Matterhorn - Build 24109
 *	
 *
 * @author		The SmitBoard Team & Community
 * @license		GNU GPL v3
 * @link		http://smitboard.sf.net
 * @since		Matterhorn
 */
 
class dbMaster
{
	protected $base;
	public $connection;
	public $result;
	public $exectime = 0;
	public $rows = array();
	public $queries = array();
	public $query_count = 0;
	public $version = NULL;

	public $prefix;

	protected $q_select = array();
	protected $q_update;
	protected $q_delete = FALSE;
	// ----------------------
	protected $q_from = array();
	protected $q_join = array();
	protected $q_where = array();
	protected $q_order = array();
	protected $q_limit = array();
	
	public $query;
	public $config = array();

	function __construct($host = 'localhost', $port = 0, $user = '', $password = '', $name = '', $prefix = 'smit_', $persist = FALSE)
	{
		// If overriding, please put parent::__construct() in your driver constructor
		$this->base = SmitBase::getInstance();
		/*if(!isset($this->base->vars['root']))
		{
			$this->base->vars['root'] = array(
				'db_host' = $host,
				'db_port' => $port,
				'db_user' => $user,
				'db_password' => $password,
				'db_name' => $name,
				'db_prefix' => $prefix,
				'db_persist' => $persist
			);
		}*/
		
		if(!$this->can_run())
		{
			if(defined('RUN_GENERIC')) return FALSE;
			$this->base->error('Unable to load database driver "' . $this->driver . '" due to a missing dependency "' . $this->dependency . '"', E_DATABASE, mysql_errno());
		}
		
		$this->base->debug->sql('Using the <span class="b">' . $this->driver . ' ' .  $this->driver_version . '</span> Driver, Though PHP\'s <span class="b">' . $this->dependency . '</span> Extension');
	}

	function connect()
	{
		die('The following function is missing in your database driver: connect()');
	}
	
	function escape($input)
	{
		die('The following function is missing in your database driver: escape()');
	}

	function execute($query)
	{
		die('The following function is missing in your database driver: execute()');
	}
	
	function run($count = TRUE)
	{
		$this->execute($this->buildquery(), $count);
		$this->query = '';
		
		$this->q_select = array();
		$this->q_update = NULL;
		$this->q_delete = FALSE;
		$this->q_from = array();
		$this->q_where = array();
		$this->q_order = array();
		$this->q_limit = array();
		
		return $this;
	}

	function version()
	{
		die('The following function is missing in your database driver: version()');
	}

	function can_run()
	{
		$extensions = array();
		foreach (get_loaded_extensions() as $ext)
		{
			$extensions[] = strtolower($ext);
		}
		return in_array($this->dependency, $extensions);
	}

	function buildquery()
	{
		die('The following function is missing in your database driver: buildquery()');
	}

	function select($fields = '*', $enclose = TRUE)
	{
		$fields = explode(',', $fields);

		foreach($fields as $field)
		{
			if(strlen($field) != 0)
			{
				$this->q_select[] = ($enclose && trim($field) != '*' ? $this->l_delim : '') . trim($field) . ($enclose && trim($field) != '*' ? $this->r_delim : '');
			}
		}
		
		return $this;
	}
	
	function from($tables, $enclose = TRUE)
	{
		$tables = explode(',', $tables);
		
		foreach($tables as $table) {
			$this->q_from[] = ($enclose ? $this->l_delim : '') . $this->prefix . trim($table) . ($enclose ? $this->l_delim : '');
		}

		return $this;
	}

	function join($table, $enclose = TRUE)
	{
		$tables = explode(',', $tables);
		
		foreach($tables as $table) {
			$this->q_join[] = ($enclose ? $this->l_delim : '') . $this->prefix . trim($table) . ($enclose ? $this->l_delim : '');
		}

		return $this;
	}
	
	function where($primary, $value = '', $operator = 'eq')
	{
		if(is_string($primary))
		{
			$primary = trim($primary);
			$operator = $this->get_operator($operator);
			$value = $this->escape($value);
			
			$this->q_where[$primary] = array((string) $value, $operator);
		}
		elseif(is_array($primary))
		{
			foreach($primary as $field => $options)
			{
				if(is_array($options))
				{
					$this->q_where[trim($field)] = array((string) $options[0], $this->get_operator($options[1]));
				}
				else
				{
					$this->q_where[trim($field)] = array((string) $options, $this->get_operator('eq'));
				}
			}
		}

		return $this;
	}
	
	function orderby($field, $order)
	{
		$order = $this->get_order($order);
		$this->q_order = array(trim($field), $order);
		
		return $this;
	}
	function limit($range, $results = 0)
	{
		if($results == 0)
		{
			$this->q_limit = array('range' => 0, 'results' => intval($range));
		}
		else
		{
			$this->q_limit = array('range' => intval($range), 'results' => intval($results));
		}

		return $this;
	}
	
	function delete()
	{
		$this->q_delete = TRUE;
		
		return $this;
	}
	
	function get_order($order)
	{
		switch(strtolower($order))
		{
			case'asc':
				$order = 'ASC';
			break;

			case'desc':
				$order = 'DESC';
			break;
			
			case 'random':
			case 'rand':
				$order = (rand(1, 6) >= 3) ? 'ASC' : 'DESC';
			break;
		}
		return $order;
	}
	
	function get_operator($operator)
	{
		switch($operator)
		{
			default:
			case'eq':
			$operator = '=';
			break;
			
			case'gt':
			$operator = '>';
			break;
			
			case'gte':
			$operator = '>=';
			break;
			
			case'lt':
			$operator = '<';
			break;
			
			case'lte':
			$operator = '<=';
			break;
			
			case'ne':
			$operator = '!=';
			break;
			
			case'like':
			$operator = 'LIKE';
			break;
		}
		
		return $operator;
	}

	function num_rows($result = NULL)
	{
		die('The following function is missing in your database driver: num_rows()');
	}	

	function affected_rows()
	{
		die('The following function is missing in your database driver: affected_rows()');
	}

	function result($result = NULL, $type = -1)
	{
		die('The following function is missing in your database driver: result()');
	}

	function results($result = NULL, $type = -1)
	{
		return $this->result($result, $type);
	}

	function row($field = NULL, $result = NULL)
	{
		$rows = $this->result($result);
		
		if($field != NULL)
		{
			return $rows[0][$field];
		}
		return $rows[0];
	}
	
	function insert($table, $data)
	{
		die('The following function is missing in your database driver: insert()');
	}

	function update($table, $data)
	{
		die('The following function is missing in your database driver: update()');
	}

	function loadconfig()
	{
		$this->config = ($this->base->cache->get('config') === FALSE) ? $this->config : $this->base->cache->get('config');
		if(count($this->config) == 0)
		{
			$this->select('key, value')->from('config')->run();
			foreach($this->result() as $config)
			{
				$this->config[$config['key']] = $config['value'];
			}
		}
		if($this->base->cache->get('config') === FALSE) $this->base->cache->add('config', $this->config, 3600);
		return $this->config;
	}
}

?>